package accs.model.io;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;

import accs.model.UserPasswordPair;

public class SeparatedUserPasswordReader extends UserPasswordReader {
	private String usernameMark;
	private String passwordMark;
	
	private BufferedReader reader;
	
	public SeparatedUserPasswordReader(Reader reader, String usernameMark, String passwordMark) {
		this.reader=new BufferedReader(reader);
		this.usernameMark=usernameMark;
		this.passwordMark=passwordMark;
	}
	
	public UserPasswordPair read() throws IOException {
		String line=null;
		String user=null, pass=null;
		
		while((line=reader.readLine())!=null && (user==null || pass==null)) {
			if(line.startsWith(usernameMark)) {
				user=line.substring(usernameMark.length());
			} else if(line.startsWith(passwordMark)) {
				pass=line.substring(passwordMark.length());
			}
		}
		
		if(user!=null && pass!=null) return new UserPasswordPair(user, pass);
		else return null;
	}
	
	public void close() throws IOException {
		reader.close();
	}
}